package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: hsl
 * @CreateTime: 2025-03-10  13:42
 */
@Slf4j
@Service("1003")
public class VipStrategy implements ItemTypeStrategy {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserVipServiceMapper userVipServiceMapper;
    @Resource
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 声明一个变量  进行返回结果赋值
        int result = 0;
        if (SystemConstant.ORDER_ITEM_TYPE_VIP.equals(userPaidRecordVo.getItemType())) {
            //  买的是vip
            // 判断 是首次购买  还是进行续期
            // 用什么进行判断  根据过期时间进行判断
            // vipID  是啥
            Long vipServiceConfigId = userPaidRecordVo.getItemIdList().get(0);
            // 获取购买时间
            VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceConfigId);
            Integer serviceMonth = vipServiceConfig.getServiceMonth();
            //  未过期的vip才能进行续期
            // 获取信息
            UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
            // 声明过期时间   用户后续过期时间赋值使用
            Date vipExpireTime = new Date();
            if (1 == userInfo.getIsVip() && userInfo.getVipExpireTime().after(new Date())) {
                //   未过期  进行续期
                // 原来的过期时间上加上 又购买的时间即可
                vipExpireTime = userInfo.getVipExpireTime();
                // 日期如何进行计算  使用JDK8 自带的 进行即可 注意每一步用变量进行接收
                LocalDateTime localDateTime = new LocalDateTime(vipExpireTime).plusMonths(serviceMonth);
//                int i = 1/0;
                //  设置新的过期时间
                vipExpireTime = localDateTime.toDate();
            } else {
                //  首次购买vip
                // 过期时间就是当前系统时间加上购买时间
                vipExpireTime = new LocalDateTime(vipExpireTime).plusMonths(serviceMonth).toDate();
            }
            // 添加对象 进行赋值
            UserVipService userVipService = new UserVipService();
            userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
            userVipService.setUserId(userPaidRecordVo.getUserId());
            userVipService.setStartTime(new Date());
            // 过期时间
            userVipService.setExpireTime(vipExpireTime);
            // 数据保存
            result += userVipServiceMapper.insert(userVipService);
            // user_info  进行更新
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(vipExpireTime);
            result += userInfoMapper.updateById(userInfo);
        }
        // 数据返回
        return result;
    }
}
